doValidation = function(id){
    var url = '/car/validate';
    var data = {};
    $('#formAdd input').each(function(){
        data[$(this).attr('name')] = $(this).val();
    });
    $('#formAdd select').each(function(){
        data[$(this).attr('name')] = $(this).val();
    });
    $.post(url, data, function(resp){
        $("#"+id).parent().find('.errors').remove();
        if ( typeof resp[id] !== "undefined" && resp[id]) {
            $("#"+id).parent().append(getErrorHtml($(resp).attr(id), id));
        }

    }, 'json');
};

function getErrorHtml(formErrors , id)
{
    var o = '<ul id="errors-'+id+'" class="errors">';
    for(errorKey in formErrors)
    {
     //   console.log(errorKey);
        o += '<li>' + formErrors[errorKey] + '</li>';
    }
    o += '</ul>';
    return o;
}

$(document).ready(function() {
    jQuery.validator.addMethod("phone", function(phone_number, element) {
        phone_number = phone_number.replace(/[-\s+]/g, "");
        return this.optional(element) || phone_number.length == 9 &&
                phone_number.match(/^\d{9}$/);
    }, "Введите корректный номер телефона");
    jQuery.validator.addMethod("selected", function(selected_value, element) {
        return this.optional(element) || selected_value > 0;
    }, "Поле обязательное");
});

/**
 * Validation add car form without ajax
 */
$(document).ready(function() {
    $("#formAdd").validate({
        errorElement: "span",
        errorClass: "errors-box",
//        success: function(label) {
//			label.addClass("success");
//		},
        rules: {
            phone: {
                required: true,
                phone: true
            },
            price:{
                number:true,
                required:true,
                min:1
            },
            engine_volume:{
                number:true,
                required:true,
                min:1
            },
            mileage:{
                number:true,
                required:true
            },
            engine_power:{
                number:true
            },
            mark: {
                selected:true
            },
            state: {
                selected:true
            },
            city: {
                selected:true
            },
            year: {
                selected:true
            },
            gear: {
                selected:true
            },
            fuel: {
                selected:true
            },
            drive: {
                selected:true
            },
            model: {
                selected:true
            }
        },
        debug:false}
            );
});
